package com.example.snuposter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PosterAdapter extends ArrayAdapter<Row> implements OnClickListener {
	
	Context context;
	int layoutResourceId;
	Row data[] = null;
	
	public PosterAdapter(Context context, int layoutResourceId, Row[] data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder = (row == null) ? new ViewHolder() : (ViewHolder) row.getTag(R.id.holder);
		final Row poster_row = data[position];
		
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
			
			holder.leftPoster = (ImageView) row.findViewById(R.id.left_image);
			holder.rightPoster = (ImageView) row.findViewById(R.id.right_image);
			
			holder.leftTitle = (TextView) row.findViewById(R.id.left_title);
			holder.rightTitle = (TextView) row.findViewById(R.id.right_title);
			
			row.setTag(R.id.holder, holder);		//set holder tag to improve performance(reduce findViewById)
			row.setTag(R.id.position, position);	//set position tag to use at onClick
		}
		
		holder.leftPoster.setImageBitmap(poster_row.left_poster);
		holder.leftTitle.setText(poster_row.left_title);
		
		holder.rightPoster.setImageBitmap(poster_row.right_poster);
		holder.rightTitle.setText(poster_row.right_title);
		
		row.setOnClickListener(this);
		
		//perform click to set onclicklistener of left, right poster
		row.performClick();
		return row;
	}
	
	public void onClick(View v) {
		ViewHolder holder = (ViewHolder) v.getTag(R.id.holder);
		final int position = (Integer) v.getTag(R.id.position);
		
		ImageView leftPoster = (ImageView) holder.leftPoster;
		ImageView rightPoster = (ImageView) holder.rightPoster;
		
		leftPoster.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String url = data[position].left_url;
				Intent intent = new Intent(context, PosterActivity.class);
				intent.putExtra("url", url);
				context.startActivity(intent);
			}
		});
		
		rightPoster.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String url = data[position].right_url;
				Intent intent = new Intent(context, PosterActivity.class);
				intent.putExtra("url", url);
				context.startActivity(intent);
			}
		});
	}
	
	static class ViewHolder {
		ImageView leftPoster;
		ImageView rightPoster;
		
		TextView leftTitle;
		TextView rightTitle;
	}
}